package com.lottery.im.module.chat.presenter;

import android.app.Activity;
import android.text.TextUtils;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.observer.BaseListObserver;
import com.lottery.im.api.result.entity.OtherUserInfo;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.chat.view.MemberVipView;
import com.tencent.imsdk.TIMGroupMemberInfo;
import com.tencent.imsdk.TIMValueCallBack;
import com.tencent.imsdk.ext.group.TIMGroupManagerExt;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class MemberVipPresenter extends BaseFragmentPresenter<MemberVipView> {

  public MemberVipPresenter(Activity activity,
      MemberVipView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  /**
   * 通过tim获取成成员
   */
  public void getGroupMembers(String dentify) {

    iView.showLoadingDialog();

    TIMGroupManagerExt.getInstance().getGroupMembers(dentify,
        new TIMValueCallBack<List<TIMGroupMemberInfo>>() {
          @Override public void onError(int i, String s) {
            if (null == iView) return;
            iView.cancelLoadingDialog();
            iView.showToast("获取群成员失败，请稍后再试～");
          }

          @Override public void onSuccess(List<TIMGroupMemberInfo> timGroupMemberInfos) {
            if (null == iView) return;
            getUserByThirdIds(timGroupMemberInfos);
          }
        });
  }

  /**
   * 通过本地服务器 获取群成员详细信息（VIP等）
   *
   * @param timGroupMemberInfos 群成员列表
   */
  private void getUserByThirdIds(List<TIMGroupMemberInfo> timGroupMemberInfos) {

    StringBuilder ids = new StringBuilder();
    for (TIMGroupMemberInfo info : timGroupMemberInfos) {
      if (!TextUtils.isEmpty(ids.toString())) ids.append(",");
      ids.append(info.getUser());
    }
    ApiClient.getApiRetrofitInstance()
        .getUserByThirdIds(ids.toString())
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new BaseListObserver<Map<String, OtherUserInfo>>() {
          @Override public void onStart() {

          }

          @Override public void onSuccess(List<Map<String, OtherUserInfo>> t) {
            iView.getGroupMembersSuc(timGroupMemberInfos, t);
            iView.cancelLoadingDialog();
          }

          @Override public void onFail(String code, String msg) {
            iView.cancelLoadingDialog();
            iView.showToast("获取群成员失败，请稍后再试～");
          }

          @Override public void onException(String msg) {
            iView.cancelLoadingDialog();
            iView.showToast("获取群成员失败，请稍后再试～");
          }

          @Override public void onFinish() {

          }
        });
  }
}
